// pages/creat/creat.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showApp: false,
    show: false,
    orgId: '',
    typeShow: false,
    tipShow: false,
    detail: {
      "orgRecommend": 0,
      "orgIntroduce": "",
      "contents":[],
    },
    orgType: [],
    orgTypeDict: {},
    serviceShow:false,
  },
  serviceShow(){
    this.toggle("serviceShow", true);
  },
  serviceClose(e) {
    this.toggle("serviceShow", false);
    if(e.currentTarget.dataset.type ==1){
      wx.switchTab({
        url: '/pages/index/index?tabActive=1'
      })
    }
  },
  check: function() {
    this.toggle("showApp", true);
  },
  onClose: function() {
    this.toggle("showApp", false);
  },
  contact: function(e) {
    console.log(e);
  },
  clearDetail: function() {
    this.toggle("detail", {
      "orgRecommend": 0,
      "orgIntroduce": ""
    });
  },
  showType() {
    this.toggle("typeShow", true)
  },
  typeClose() {
    this.toggle("typeShow", false)
  },
  selectType(e) {
    var item = e.currentTarget.dataset.item;
    console.log('组织type',item.code)
    this.toggle("detail.orgType", item.code);
    this.toggle("typeShow", false)
    // if (3 == item.code * 1) {
    //   this.toggle("detail.orgType", item.code);
    //   this.toggle("typeShow", false)
    // } else {
    //   this.toggle("detail.orgType", '');
    //   this.toggle("typeShow", false)
    //   this.toggle("showApp", true)
    // }
  },
  validate: function() {
    var that = this;
    var detail = that.data.detail;
    var orgName = detail.orgName;
    var orgUrl = detail.orgUrl;
    if (!orgUrl) {
      // wx.showToast({
      //   title: '组织头像必传',
      //   icon: 'none',
      //   duration: 2000
      // })
      // return false;
      this.toggle("detail.orgUrl", "https://img.yiqilaiwang.com/tmp/wxa0535d31b27c14d8.o6zAJs2b9caL0xH8yzIUilWRbI6Y.tqzh3Q75Sdyv840af565712bb19a8d65a06f1d070367.png");
    }
    if (!orgName) {
      wx.showToast({
        title: '请输入组织名称',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (orgName.length < 2 || orgName.length > 16) {
      wx.showToast({
        title: '请输入正确的组织名称',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    if (!detail.orgType) {
      wx.showToast({
        title: '请选择组织类型',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    return true;
  },
  creatOver() {
    var that = this;
    var detail = that.data.detail;
    var success = that.validate();
    if (!success) {
      return;
    }
    that.nameExist(function() {
      //组织不存在的情况才会继续
      app.req({
        // url: "orgmini/org/addOrg",
        url:"orgmini/org/newAddOrg",
        data: detail,
        success: function(res) {
          if (200 == res.code) {
            var orgId = res.data.orgId;
            that.toggle("orgId", orgId);
            that.toggle("serviceShow", true);
            app.getRefresh(function (refresh) {
            }, '1')
          }
        }
      });
    });
  },
  tipClose() {
    this.toggle("tipShow", false)
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    var url="";
    if (this.data.tipShow && dataset.edit){
      return
    }
    if (dataset.edit){
      var contents = this.data.detail.contents.length ? JSON.stringify(this.data.detail.contents) : '';
      url = dataset.url + '?contents=' + contents;
    }else{
      url = dataset.url + "?id=" + this.data.orgId
    }
    wx.navigateTo({
      url: url
    })
  },
  toInform(e) {
    wx.navigateTo({
      url: '/packageA/pages/feedback/feedback'
    })
  },
  chooseImg: function() {
    var that = this;
    that.toggle("show", true);
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const file = res.tempFilePaths[0];
        that.toggle("show", true);
        wx.navigateTo({
          url: '/pages/uploadHead/uploadHead?src=' + file,
        })
      }
    })
  },
  initDict: function() {
    var that = this;
    app.req({
      url: "wechat/dictionary/listBydictType",
      data: {
        dictType: "ORG_TYPE"
      },
      success: function(res) {
        if (200 == res.code) {
          var orgType = [];
          var orgTypeDict = {};
          for (let i of res.data) {
            orgTypeDict[i.dictValue] = i.dictName;
            orgType.push({
              name: i.dictName,
              code: i.dictValue
            })
          }
          that.toggle("orgType", orgType);
          that.toggle("orgTypeDict", orgTypeDict);
        }
      }
    });
  },
  nameExist: function(callback) {
    var that = this;
    var value = that.data.detail.orgName;
    app.req({
      url: "orgmini/org/checkAddOrg",
      data: {
        orgName: value
      },
      success: function(res) {
        if (200 == res.code) {
          that.toggle("orgId", res.data.orgId);
          if (res.data.orgId) {
            that.toggle("tipShow", true);
          } else {
            callback && callback();
          }
        }
      }
    });
  },
  onDescChange: function(e) {
    this.toggle("detail.orgIntroduce", e.detail.value);
  },
  onNameChange: function(e) {
    this.toggle("detail.orgName", e.detail.value);
    this.toggle("orgName", e.detail.value);
    console.log(this.data.detail.orgName,55)
    if (e.detail.value.length < 2){
      wx.showToast({
        title: '请输入正确的组织名称',
        icon: 'none',
        duration: 2000
      })
      this.toggle("nameError",true)
    }else if (e.detail.value){
      this.toggle("nameError", false)
      this.nameExist();
    }
  },
  onSwitchChange: function(e) {
    this.toggle("detail.orgRecommend", 1 - e.detail.value * 1);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log('图文混编',options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.toggle('isShare', false);
    var that = this;
    if (that.data.show) {
      that.toggle("show", false);
      return;
    }
    app.getUserInfo(function(userInfo) {
      that.toggle("userInfo", userInfo);
      that.initDict();
      // that.clearDetail();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.toggle('isShare', false);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    this.setData({
      isShare: true
    });
    return {
      title: this.data.userInfo.realName + "邀请你加入" + this.data.orgName,
      path:'/pages/orgHome/orgHome?id='+this.data.orgId
    }　
  }
})